﻿using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.DAL
{
    /// <summary>
    /// 视频数据服务类
    /// </summary>
    public class VideoDAL : IVideoDAL
    {
        /// <summary>
        /// 数据库添加视频
        /// </summary>
        /// <param name="video"></param>
        /// <returns></returns>
        public TBVideos AddVideo(TBVideos video)
        {
            video.Status = (int)VideoStatus.Uploading;
            video.UploadTime = DateTime.Now;
            Model1 model = new Model1();
            model.Entry(video).State = EntityState.Added;
            model.SaveChanges();
            return video;
        }

        /// <summary>
        /// 获取待处理的视频id集合
        /// </summary>
        /// <returns></returns>
        public List<string> GetInProcessVideoIds()
        {
            Model1 model = new Model1();
            //待处理的视频：上传中、上传成功、转码中
            return model.TBVideos.Where(a => a.Status == (int)VideoStatus.Uploading || a.Status == (int)VideoStatus.UploadSucces || a.Status == (int)VideoStatus.Transcoding).Select(a => a.VideoId).ToList();
        }


        /// <summary>
        /// 批量更新视频信息（封面地址和状态）
        /// </summary>
        /// <param name="videos"></param>
        public void UpdateVideos(List<VideoInfo> videos)
        {
            Model1 model = new Model1();
            foreach(VideoInfo videoInfo in videos)
            {
                TBVideos video= model.TBVideos.Find(videoInfo.VideoId);
                if (!string.IsNullOrEmpty(videoInfo.CoverURL))
                {
                    video.CoverURL = videoInfo.CoverURL.Substring(0, videoInfo.CoverURL.IndexOf("?"));
                    video.Status = (int)videoInfo.Status;
                }
                model.Entry(video).State = EntityState.Modified;
                model.SaveChanges();
            }
        }

        /// <summary>
        /// 通过用户id获取视频列表
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public List<TBVideos> GetVideosByUserId(int userId)
        {
            Model1 model = new Model1();
            return model.TBVideos.Where(a => a.UserId == userId).OrderByDescending(a=>a.UploadTime).ToList();
        }

        /// <summary>
        /// 根据视频id删除视频
        /// </summary>
        /// <param name="videoId">视频id</param>
        public void DeleteVideo(string videoId)
        {
            Model1 model = new Model1();
            TBVideos video = model.TBVideos.Find(videoId);
            model.TBVideos.Remove(video);
            model.SaveChanges();
        }

        /// <summary>
        /// 获取可播放的视频列表
        /// </summary>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            Model1 model = new Model1();
            List<TBVideos> list= model.TBVideos.Where(a => a.Status == (int)VideoStatus.Normal).OrderByDescending(a => a.UploadTime).ToList(); ;
            return list;
        }

        /// <summary>
        /// 根据视频id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            Model1 model = new Model1();
            return model.TBVideos.Find(videoId);
        }
    }
}
